package com.ruoyi.web.controller.aggregation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.aggregation.MerchantProfitConfig;
import com.ruoyi.system.service.aggregation.IMerchantProfitConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家个性化分润配置Controller
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/agg/merchantConfig")
public class MerchantProfitConfigController extends BaseController
{
    @Autowired
    private IMerchantProfitConfigService merchantProfitConfigService;

    /**
     * 查询商家个性化分润配置列表
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantProfitConfig merchantProfitConfig)
    {
        startPage();
        List<MerchantProfitConfig> list = merchantProfitConfigService.selectMerchantProfitConfigList(merchantProfitConfig);
        return getDataTable(list);
    }

    /**
     * 导出商家个性化分润配置列表
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:export')")
    @Log(title = "商家个性化分润配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantProfitConfig merchantProfitConfig)
    {
        List<MerchantProfitConfig> list = merchantProfitConfigService.selectMerchantProfitConfigList(merchantProfitConfig);
        ExcelUtil<MerchantProfitConfig> util = new ExcelUtil<MerchantProfitConfig>(MerchantProfitConfig.class);
        util.exportExcel(response, list, "商家个性化分润配置数据");
    }

    /**
     * 获取商家个性化分润配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(merchantProfitConfigService.selectMerchantProfitConfigById(id));
    }

    /**
     * 根据商家ID获取分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:query')")
    @GetMapping(value = "/merchant/{merchantId}")
    public AjaxResult getInfoByMerchantId(@PathVariable("merchantId") Long merchantId)
    {
        MerchantProfitConfig config = merchantProfitConfigService.selectMerchantProfitConfigByMerchantId(merchantId);
        if (config == null) {
            return error("该商家暂无分润配置");
        }
        return success(config);
    }

    /**
     * 新增商家个性化分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:add')")
    @Log(title = "商家个性化分润配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantProfitConfig merchantProfitConfig)
    {
        try {
            return toAjax(merchantProfitConfigService.insertMerchantProfitConfig(merchantProfitConfig));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改商家个性化分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:edit')")
    @Log(title = "商家个性化分润配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantProfitConfig merchantProfitConfig)
    {
        try {
            return toAjax(merchantProfitConfigService.updateMerchantProfitConfig(merchantProfitConfig));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除商家个性化分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:remove')")
    @Log(title = "商家个性化分润配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(merchantProfitConfigService.deleteMerchantProfitConfigByIds(ids));
    }

    /**
     * 审核商家分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:audit')")
    @Log(title = "商家分润配置审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody MerchantProfitConfig merchantProfitConfig)
    {
        try {
            return toAjax(merchantProfitConfigService.auditMerchantProfitConfig(merchantProfitConfig));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 为商家创建默认分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:add')")
    @Log(title = "创建默认分润配置", businessType = BusinessType.INSERT)
    @PostMapping("/createDefault/{merchantId}")
    public AjaxResult createDefault(@PathVariable("merchantId") Long merchantId, @RequestBody(required = false) String merchantName)
    {
        try {
            // 从请求体或参数中获取商家名称
            if (merchantName == null || merchantName.trim().isEmpty()) {
                merchantName = "商家" + merchantId;
            }
            return toAjax(merchantProfitConfigService.createDefaultConfigForMerchant(merchantId, merchantName));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 获取商家异业联盟分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:query')")
    @GetMapping("/interIndustry/{merchantId}")
    public AjaxResult getInterIndustryConfig(@PathVariable("merchantId") Long merchantId)
    {
        return success(merchantProfitConfigService.getInterIndustryConfig(merchantId));
    }

    /**
     * 获取商家广告收益分润配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:query')")
    @GetMapping("/adRevenue/{merchantId}")
    public AjaxResult getAdRevenueConfig(@PathVariable("merchantId") Long merchantId)
    {
        return success(merchantProfitConfigService.getAdRevenueConfig(merchantId));
    }

    /**
     * 验证分润比例配置
     */
    @PreAuthorize("@ss.hasPermi('agg:merchantConfig:edit')")
    @PostMapping("/validate")
    public AjaxResult validateConfig(@RequestBody MerchantProfitConfig config)
    {
        boolean isValid = merchantProfitConfigService.validateProfitRates(config);
        if (isValid) {
            return success("分润比例配置验证通过");
        } else {
            return error("分润比例配置验证失败，各比例之和必须等于100%");
        }
    }
}
